﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using eTruck.Helpers;

namespace eTruck.Views.Role
{
    public partial class Form : System.Web.UI.Page
    {
        protected string returnValue = string.Empty;

        BLL.Role bll = new BLL.Role();
        protected void Page_Load(object sender, EventArgs e)
        {
            string type = System.Web.HttpUtility.UrlDecode(Request["Type"]);
            if (type == "Save")
            {
                Save();
            }
            else
            {
                Exists();
            }
        }

        private void Save()
        {
            Model.Role model = new Model.Role();
            string RoleID = System.Web.HttpUtility.UrlDecode(Request["RoleID"]);
            model.RouleName = System.Web.HttpUtility.UrlDecode(Request["RouleName"]);
            model.Desc = System.Web.HttpUtility.UrlDecode(Request["Desc"]);

            try
            {
                model.UserID = Convert.ToInt32(CookiesHelper.GetCookie(CommonHelper.LoginCookieName));
            }
            catch { }

            try
            {
                bool result = false;
                if (RoleID.Length == 0)
                {
                    //create new model
                    result = bll.Add(model);
                }
                else
                {
                    //update model Info...
                    model.RoleID = Convert.ToInt32(RoleID);
                    result = bll.Update(model);
                }
                if (result)
                {
                    returnValue = "True";
                }
                else
                {
                    returnValue = "False";
                }
            }
            catch (Exception ex)
            {
                returnValue = ex.Message;
            }
        }

        private void Exists()
        {
            string RouleName = System.Web.HttpUtility.UrlDecode(Request["RouleName"]);
            if (bll.Exists(RouleName))
            {
                returnValue = "True";
            }
            else
            {
                returnValue = "False";
            }
        }
    }
}